use serde::Deserialize;

// Validate 派生将暗示的特征
use validator::{Validate, ValidationError};

// 数据验证（表单/API 参数）
// https://crates.io/crates/validator
// cargo add validator --features derive
#[derive(Debug, Validate, Deserialize)]
struct SignupData {
    #[validate(email)]
    mail: String,
    #[validate(url)]
    site: String,
    #[validate(length(min = 1), custom(function = "validate_unique_username"))]
    #[serde(rename = "firstName")]
    first_name: String,
    #[validate(range(min = 18, max = 20))]
    age: u32,
    #[validate(range(exclusive_min = 0.0, max = 100.0))]
    height: f32,
}

// 自定义数据校验处理函数
fn validate_unique_username(username: &str) -> Result<(), ValidationError> {
    if username == "xXxShad0wxXx" {
        // 用户名的值将在稍后自动添加
        return Err(ValidationError::new("terrible_username"));
    }

    Ok(())
}
